def hate(text):
    # encoding:utf-8
    import requests

    # client_id 为官网获取的AK， client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=aVHeivQ6AvBURA9SIEFFGoO9&client_secret=YePt7g6MdZlA9HTOKDCx6wu2YnCACCrD'
    response = requests.get(host)
    request_url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined"

    params = {"text": text}
    access_token = response.json().get('access_token')
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        # print(response.json())
        conclusion = response.json().get('conclusion')
        if conclusion == '不合规':
            return response.json().get('data')[0].get('msg')
        else:
            return '合规'


import requests
import json

API_KEY = "bRlSKGjvciowfGX2MyEJkqel"
SECRET_KEY = "BA3ONFxJfats2LFiALBpVI70KKD3fuqz"


def emotion(text):
    url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?charset=UTF-8&access_token=" + get_access_token()

    payload = json.dumps({
        "text": text
    })
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    # {'confidence': 0.999809, 'negative_prob': 0.999914, 'positive_prob': 8.5838e-05, 'sentiment': 0}
    return response.json().get('items')[0]


def get_access_token():
    """
    使用 AK，SK 生成鉴权签名（Access Token）
    :return: access_token，或是None(如果错误)
    """
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    return str(requests.post(url, params=params).json().get("access_token"))


if __name__ == '__main__':
    print(emotion("？"))
